﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using prjBULHRM;
namespace prjQuanLyNhanSu
{
    public partial class frmCa : Form
    {
        public frmCa()
        {
            InitializeComponent();
        }
        clsBULHT_Ca cls;
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "&Thêm")
            {               
                    btnThem.Text = "&Lưu Thêm";
                    txtMaCa.Text = "";
                    txtGioBD.Text = "";
                    txtPhutBD.Text = "";
                    txtTenCa.Text = "";
                    txtGioKT.Text = "";
                    txtPhutKT.Text = "";   
                    txtMaCa.Enabled = true;
                    txtGioBD.Enabled = true;
                    txtPhutBD.Enabled = true;
                    txtTenCa.Enabled = true;
                    txtGioKT.Enabled = true;
                    txtPhutKT.Enabled = true;
                    txtMaCa.Focus();
                    btnSua.Enabled = false;
                    btnXoa.Enabled = false;
            }
            else
            {
                if (txtMaCa.Text == "" || txtGioBD.Text == ""||txtPhutBD.Text=="" || txtTenCa.Text == "" || txtGioKT.Text == ""||txtPhutKT.Text=="")
                {
                    MessageBox.Show("Chưa đủ thông tin !", "Lỗi dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    btnThem.Text = "&Thêm";
                    cls = new clsBULHT_Ca();
                    cls.CaID = txtMaCa.Text;
                    cls.TenCa = txtTenCa.Text;
                    cls.GioBD = txtGioBD.Text+","+txtPhutBD.Text;
                    cls.GioKT = txtGioKT.Text+","+txtPhutKT.Text;
                    if(cls.TrungMa()==true)//true la trung
                    {
                        MessageBox.Show("Trùng mã !","Lỗi Trùng",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    }
                    else
                    {
                        cls.Insert();
                        LoadDuLieu();
                        MessageBox.Show("Thêm thành công !");
                        btnSua.Enabled = true;
                        btnXoa.Enabled = true;
                    }
                }
            }
        }
        
        private void LoadDuLieu()
        {
            //String str=new String();
            cls = new clsBULHT_Ca();
            DataTable tbl = cls.SelectAll();
            DataTable btl2=new DataTable();
            foreach (DataRow row in tbl.Rows)
            {
                string s = row["GioBD"].ToString().Trim();
                string p = s.Substring(s.IndexOf(',')+ 1, 2);
                string h = s.Substring(0, s.Length - p.Length-1);
                row["GioBD"] = h + "h" + p + "'";

                string k = row["GioKT"].ToString().Trim();
                string k1 = k.Substring(k.IndexOf(',') + 1, 2);
                string k2 = k.Substring(0, k.Length - k1.Length - 1);
                row["GioKT"] = k2 + "h" + k1 + "'";
            }
           
            dgrCa.DataSource = tbl;
            txtMaCa.Enabled = false;
            txtGioBD.Enabled = false;
            txtTenCa.Enabled = false;
            txtGioKT.Enabled = false;
            txtPhutBD.Enabled = false;
            txtPhutKT.Enabled = false;
            btnThem.Text = "&Thêm";
            btnSua.Text = "&Sửa";           
        }

        private void frmCa_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
            //dgrCa.CurrentRow.Cells[2].Value = txtGioBD.Text + "h" + txtPhutBD + "'";
            //dgrCa.CurrentRow.Cells[3].Value=txtGioKT.Text+"h"+txtPhutKT.Text+"'";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "&Sửa")
            {
                if (txtMaCa.Text == "")
                {
                    MessageBox.Show("Bạn chưa chọn dữ liệu để sửa!", "Lỗi Dữ Liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    btnSua.Text = "&Lưu Sửa";
                    txtMaCa.Enabled = false; ;
                    txtGioBD.Focus();
                    txtGioBD.Enabled = true;
                    txtPhutBD.Enabled = true;
                    txtTenCa.Enabled = true;
                    txtGioKT.Enabled = true;
                    txtPhutKT.Enabled = true;
                    btnXoa.Enabled = false;
                    btnThem.Enabled = false;
                }
            }
            else
            {
                try
                {
                    cls = new clsBULHT_Ca();
                    btnSua.Text = "&Sửa";
                    cls.CaID = txtMaCa.Text;
                    cls.TenCa = txtTenCa.Text; 
                    cls.GioBD = txtGioBD.Text+","+txtPhutBD.Text;
                    cls.GioKT = txtGioKT.Text+","+txtPhutKT.Text;
                    if (txtMaCa.Text == "" | txtGioBD.Text == ""||txtPhutBD.Text=="" || txtTenCa.Text == "" || txtGioKT.Text == ""||txtPhutKT.Text=="")
                    {
                        MessageBox.Show("Không được rỗng !");
                    }
                    else
                    {
                        cls.Update();                        
                        LoadDuLieu();
                        MessageBox.Show("Sửa thành công !");
                        btnXoa.Enabled = true;
                        btnThem.Enabled = true;
                    }
                }
                catch (Exception er)
                {
                    MessageBox.Show("Không cho sửa vì đã dữ liệu này đã tồn tại!");
                }
            }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaCa.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa.","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else
            {
                DialogResult hoi = MessageBox.Show("Bạn có chắc xóa dữ liệu này không?", "Xóa dữ liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (hoi == DialogResult.Yes)
                {
                    cls = new clsBULHT_Ca();
                    cls.CaID = txtMaCa.Text;
                    try
                    {
                        cls.Delete();
                        LoadDuLieu();
                        txtMaCa.Text = "";
                        txtGioBD.Text = "";
                        txtPhutBD.Text = "";
                        txtTenCa.Text = "";
                        txtGioKT.Text = "";
                        txtPhutKT.Text = "";
                        MessageBox.Show("Xóa thành công!");
                    }
                    catch (Exception er)
                    {
                        MessageBox.Show("Dữ liệu này không xóa được!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void dgrCa_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaCa.Text = dgrCa.CurrentRow.Cells[0].Value.ToString();
            txtTenCa.Text = dgrCa.CurrentRow.Cells[1].Value.ToString();
            string s=dgrCa.CurrentRow.Cells[2].Value.ToString();
            string p = s.Substring(s.IndexOf('h')+1, 2);
            string h = s.Substring(0, s.Length - p.Length-2);
            txtPhutBD.Text = p;
            txtGioBD.Text=h;

            string k=dgrCa.CurrentRow.Cells[3].Value.ToString();
            string k1 = k.Substring(k.IndexOf('h') + 1, 2);
            string k2 = k.Substring(0, k.Length - k1.Length - 2);
            txtGioKT.Text = k2;
            txtPhutKT.Text = k1;
        }

        private void dgrCa_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            btnThem.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;

            txtMaCa.Enabled = false;
            txtGioBD.Enabled = false;
            txtTenCa.Enabled = false;
            txtGioKT.Enabled = false;

            btnThem.Text = "&Thêm";
            btnSua.Text = "&Sửa";
        }

        private void txtGioBD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(!char.IsControl(e.KeyChar)&& !char.IsDigit(e.KeyChar))
                e.Handled=true;
        }

        private void txtPhutBD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txtGioKT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txtPhutKT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                e.Handled = true;
        }
    }
}
